package com.loren.demo10.controller;

import com.loren.demo10.aspect.Log;
import com.loren.demo10.service.TestService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class TestController {

    private final TestService testService;

    @Log
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/hello2")
    public String hello2() {
        Random random = new Random();
        int i = random.nextInt(5);
        if (i == 2) {
            throw new RuntimeException("随机数不能为2");
        }
        return "hello2";
    }

    @GetMapping("/param")
    public String param() {
        Map<String, Object> mapInfo = new HashMap<>();
        mapInfo.put("name", "Loren");
        this.testService.testParams(100L, mapInfo);
        return "hello param";
    }

    @GetMapping("/script")
    public Object script() {
        return this.testService.testScript(100L);
    }
}
